package ajax;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.mysql.cj.xdevapi.JsonArray;
import entity.Userinfo;
import service.serviceimpl.ServiceUserImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

/**
 * @Author wuchao66
 * @Date 2023/5/23 10:25
 * @PackageName:${PACKAGE_NAME}
 * @ClassName: ${NAME}
 * @Description: TODO
 * @Param
 * @Version 1.0
 */
@WebServlet(name = "ServletAjax11",urlPatterns = "/ServletAjax11")
public class ServletAjax11 extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();//文件流
        String op = request.getParameter("op");
        String name = request.getParameter("name");
        ServiceUserImpl serviceUser = new ServiceUserImpl();
        if(op.equals("find")){
            int i=   serviceUser.load("select count(1) from userinfo where name=?",name);
            if(i>0){
              String xxxx=  JSON.toJSONString("y");
                out.print(xxxx);
            }else{
                out.print(JSON.toJSONString("N"));
            }
        }else  if(op.equals("list")){
            List<Userinfo> userinfos = serviceUser.executeQuery("select * from userinfo where name like ?", "%" + name + "%");
            //序列化上车 xml html json
           // String s = JSON.toJSONString(userinfos,SerializerFeature.PrettyFormat,SerializerFeature.WriteMapNullValue,SerializerFeature.NotWriteDefaultValue);
            String s = JSON.toJSONString(userinfos);
            //Date date = new Date();
           // JSON.toJSONStringWithDateFormat(date,"yyyy-MM-dd HH:mm:ss");
            out.print(s);
        }else  if(op.equals("xulie")){
            int i=   serviceUser.load("select count(1) from userinfo where name=?",name);
            if(i>0){
                String xxxx=  JSON.toJSONString("y");
                out.print(xxxx);
            }else{
                out.print(JSON.toJSONString("N"));
            }
        }

        out.flush();
        out.close();
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();//文件流

        out.flush();
        out.close();
    }


}
